package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 套餐管理
 */
@RestController("adminSetmealController")
@RequestMapping("/admin/setmeal")
@Api(tags = "套餐相关接口")
@Slf4j
public class SetmealContruller {
    @Autowired
    private SetmealService setmealService;

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        log.info("分页查询, {}", setmealPageQueryDTO);
        PageResult pageResult = setmealService.page(setmealPageQueryDTO);

        return Result.success(pageResult);
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @Caching(evict = {
            @CacheEvict(cacheNames = "setmealCache", key = "#setmealDTO.categoryId") ,//删除redis缓存
            @CacheEvict(cacheNames = "setmeal_DishCache", allEntries = true) //删除所有的套餐包含菜品的redis缓存数据
    })
    public Result<String> save(@RequestBody SetmealDTO setmealDTO) {
        log.info("新增菜品, {}", setmealDTO);
        setmealService.saveWithDish(setmealDTO);

        return Result.success();
    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        log.info("根据id查询菜品, {}", id);
        SetmealVO setmealVO = setmealService.getById(id);

        return Result.success(setmealVO);
    }

    /**
     * 修改套餐及关联的菜品
     *
     * @param setmealDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐")
    @Caching(evict = {  //删除所有的套餐redis缓存数据
            @CacheEvict(cacheNames = "setmealCache", allEntries = true),
            @CacheEvict(cacheNames = "setmeal_DishCache", allEntries = true)
    })
    public Result<String> updateWithDishes(@RequestBody SetmealDTO setmealDTO) {
        log.info("修改套餐, {}", setmealDTO);
        setmealService.updateWithDishes(setmealDTO);

        return Result.success();
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @Caching(evict = {  //删除所有的套餐redis缓存数据
            @CacheEvict(cacheNames = "setmealCache", allEntries = true),
            @CacheEvict(cacheNames = "setmeal_DishCache", allEntries = true)
    })
    public Result<String> deleteByIds(@RequestParam List<Long> ids) {
        log.info("批量删除套餐, {}", ids);
        for (Long id : ids) {
            //判断套餐是否处于起售中
            if (setmealService.getById(id).getStatus().equals(StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }

        //批量删除套餐
        setmealService.deleteByIds(ids);

        return Result.success();
    }

    /**
     * 套餐起售、停售
     *
     * @param id
     * @param status
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售、停售")
    @Caching(evict = {  //删除所有的套餐redis缓存数据
            @CacheEvict(cacheNames = "setmealCache", allEntries = true),
            @CacheEvict(cacheNames = "setmeal_DishCache", allEntries = true)
    })
    public Result<String> startOrStop(Long id, @PathVariable Integer status) {
        log.info("套餐起售、停售, 套餐id:{}, 状态: {}", id, status);
        setmealService.startOrStop(id, status);

        return Result.success();
    }
}
